#include <iostream>
#include <set>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	set<int>b[n+1];
	for(int i = 0;i<n;++i){
		cin>>a[i];
	}
	for(int i = 1;i<=n;++i){
		b[i].insert(i);
	}
	int j = 1;
	while(++j){
		for(int i = 0;i<n;++i){
			for(set<int>::iterator it = b[i+1].begin();it!=b[i+1].end();++it){
				if((*it)==a[i]){
					cout<<j;
					return 0;
				}
				b[a[i]].insert((*it));
			}
		}
	}
	return 0;
}
/*
5
2 4 2 3 1

*/